<template>
  <div>
    <el-autocomplete
      class="inline-input"
      v-model="state1"
      clearable
      :fetch-suggestions="querySearch"
      placeholder="请输入内容"
      @select="handleSelect"
    ></el-autocomplete>
  </div>
</template>

 <script>
import paylist from "@/api/paylist";
export default {
  props: {
    type: 0
  },
  data() {
    return { restaurants: [], state1: "" };
  },
  mounted() {
    console.log(this.type);
    this.list();
  },
  watch: {
    state1(val) {
      console.log(val);
      if (val == "") {
        this.$emit("childfun", { id: "" });
      }
    }
  },
  methods: {
    querySearch(queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) ===
          0
        );
      };
    },
    handleSelect(item) {
      this.$emit("childfun", item);
    },
    list() {
      if (this.type == 1) {
        paylist.findHospitalList().then(res => {
          console.log(res);
          if (res.data.code == 0) {
            let datas = res.data.data.list;
            datas.map(res => {
              let obj = {
                value: res.name,
                id: res.hospitalInformationId
              };
              this.restaurants.push(obj);
            });
          } else {
            this.$message.error("医院拉取失败");
          }
        });
      } else if (this.type == 2){
        paylist.hospitalNameDown().then(res => {
          console.log(res);
          if (res.data.code == 0) {
            let datas = res.data.data;
            datas.map(res => {
              let obj = {
                value: res.name,
                id: res.id
              };
              this.restaurants.push(obj);
            });
          } else {
            this.$message.error("医院拉取失败");
          }
        });
      } else {
        paylist.hostlist().then(res => {
          console.log(res);
          if (res.data.code == 0) {
            let datas = res.data.data;
            datas.map(res => {
              let obj = {
                value: res.name,
                id: res.id
              };
              this.restaurants.push(obj);
            });
          } else {
            this.$message.error("医院拉取失败");
          }
        });
      }
    }
  }
};
</script>